package com.study.manage.repository;

import com.study.manage.entity.PermissionVO;
import com.study.manage.entity.UserRolesVO;
import com.study.manage.pojo.Permission;
import com.study.manage.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.List;


@Component
public interface PermissionRepository extends JpaRepository<Permission,Integer>, JpaSpecificationExecutor<Permission> {

    @Modifying
    @Transactional
    @Query("update Permission a set " +
            "a.name = CASE WHEN :#{#user.name} IS NULL THEN a.name ELSE :#{#user.name} END ," +
            "a.pid = CASE WHEN :#{#user.pid} IS NULL THEN a.pid ELSE :#{#user.pid} END ," +
            "a.zindex = CASE WHEN :#{#user.zindex} IS NULL THEN a.zindex ELSE :#{#user.zindex} END ," +
            "a.istype = CASE WHEN :#{#user.istype} IS NULL THEN a.istype ELSE :#{#user.istype} END ," +
            "a.descpt = CASE WHEN :#{#user.descpt} IS NULL THEN a.descpt ELSE :#{#user.descpt} END ," +
            "a.insertTime = CASE WHEN :#{#user.insertTime} IS NULL THEN a.insertTime ELSE :#{#user.insertTime} END ," +
            "a.updateTime = CASE WHEN :#{#user.updateTime} IS NULL THEN a.updateTime ELSE :#{#user.updateTime} END ," +
            "a.code = CASE WHEN :#{#user.code} IS NULL THEN a.code ELSE :#{#user.code} END ," +
            "a.icon = CASE WHEN :#{#user.icon} IS NULL THEN a.icon ELSE :#{#user.icon} END ," +
            "a.page =  CASE WHEN :#{#user.page} IS NULL THEN a.page ELSE :#{#user.page} END " +
            "where a.id = :#{#user.id}")
    int update(@Param("user") Permission permission);

    List<Permission> findByPid(Integer pid);


    @Query("select  new com.study.manage.entity.PermissionVO(r.id, r.name,r.pid, r.istype, r.code, r.page, r.icon, r.zindex)" +
            " from Permission r")
    List<PermissionVO> findPerms();

}
